import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import Collection from 'ol/Collection';
import TileLayer from "ol/layer/Tile";
import XYZ from 'ol/source/XYZ'
import { Style, Fill, Text } from 'ol/style';



import { get as getProjection } from "ol/proj";
import { getWidth, getTopLeft } from "ol/extent";
 
const projection = getProjection("EPSG:4326");
const projectionExtent = projection.getExtent();
const size = getWidth(projectionExtent) / 256;
const resolutions = new Array(19);
const matrixIds = new Array(19);
for (let z = 0; z < 19; ++z) {
  resolutions[z] = size / Math.pow(2, z);
  matrixIds[z] = z;
}
import WMTS from "ol/source/WMTS";
import WMTSTileGrid from "ol/tilegrid/WMTS"
const tileGrid = new WMTSTileGrid({
      origin: getTopLeft(projectionExtent),
      resolutions: resolutions,
      matrixIds: matrixIds,
    });
    

const SOURCE_Z = 1200;
const PARTITIONS_Z = 2;
const BASE_MARKER_Z = 10;
const BASE_Z = 1;
const FIELD_Z = 3;
const FIELD_TEXT_Z = 7;
const FIELD_CENTER_Z = 2;
const ROOM_Z = 6;
const ROOM_TEXT_Z = 7;
const ROOM_CENTER_Z = 7;

const FACILITIES_Z = 8;
const DEVICE_Z = 8;




/**
 * 地图上所有图层的集合，创建地图时获取一个MapLayer类，得到所有的Layer。
 */
class MapLayer {
      constructor() {
            this.drawingFeatures = new Collection(); // 添加的时候.修改边界的Feature集合

            /**
             * 临时图层,在规划的Feature未保存到数据库之前,临时存放
             * @type {VectorSource}
             */
            this.drawSource = new VectorSource({ features: this.drawingFeatures, wrapX: false });
            this.drawVector = new VectorLayer({
                  source: this.drawSource,
            });
            this.drawVector.setZIndex(SOURCE_Z);

            

            this.imgLayer2 = new TileLayer({
                  source: new WMTS({
                        url: "http://t0.tianditu.gov.cn/img_c/wmts?tk=821f44ce5a8ae682b25af7b45443a9dd",
                        layer: "img",
                        style: "default",
                        matrixSet: "c",
                        format: "tiles",
                        wrapX: true,
                        tileGrid: tileGrid,
                        requestEncoding: "KVP",
                        attributions: [
                              '&copy; <a target="_blank" href="http://www.tianditu.gov.cn/">天地图</a>',
                        ],
                        tilePixelRatio: 1,
                      }),
            })

            

            this.imgLayer3 = new TileLayer({
                  source: new WMTS({
                        url: "http://t0.tianditu.gov.cn/cia_c/wmts?tk=821f44ce5a8ae682b25af7b45443a9dd",
                        layer: "cia",
                        style: "default",
                        matrixSet: "c",
                        format: "tiles",
                        wrapX: true,
                        tileGrid: tileGrid,
                      }),
            })


            
            this.imgLayer4 = new TileLayer({
                  source: new WMTS({
                        url: "http://t0.tianditu.gov.cn/vec_c/wmts?tk=821f44ce5a8ae682b25af7b45443a9dd",
                        layer: "vec",
                        style: "default",
                        matrixSet: "c",
                        format: "tiles",
                        wrapX: true,
                        tileGrid: tileGrid,
                        requestEncoding: "KVP",
                        attributions: [
                              '&copy; <a target="_blank" href="http://www.tianditu.gov.cn/">天地图</a>',
                        ],
                        tilePixelRatio: 1,
                      }),
            })

            

            this.imgLayer5 = new TileLayer({
                  source: new WMTS({
                        url: "http://t0.tianditu.gov.cn/cva_c/wmts?tk=821f44ce5a8ae682b25af7b45443a9dd",
                        layer: "cva",
                        style: "default",
                        matrixSet: "c",
                        format: "tiles",
                        wrapX: true,
                        tileGrid: tileGrid,
                      }),
            })

            


            /**
             * 卫星图层
             * @private 
             */
            this.satellite = new TileLayer({
                  source: new XYZ({
                        url: 'http://t0.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=821f44ce5a8ae682b25af7b45443a9dd',
                        maxZoom: 20,
                  }),
            });
            /**
             * 卫星图层
             * @private
             */
            this.base = new TileLayer({
                  source: new XYZ({
                        url: 'http://t0.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=821f44ce5a8ae682b25af7b45443a9dd',
                        maxZoom: 20,
                  }),
            });

            
            /**
             * 地块矢量图层
             */
            this.fieldSource = new VectorSource();
            this.fieldVector = new VectorLayer({
                  source: this.fieldSource
            });
            this.fieldVector.setZIndex(FIELD_Z);

      }

      /**
       * 获取所有的图层
       * @public
       * @return {Array<import('ol/layer').Vector> }
       */
      getAllLayers() {
            return [
                  
                  
                  this.imgLayer4,
                  this.imgLayer5,
                  //this.base, 
                  //this.satellite,
                  this.fieldVector,
            ];
      }
}



export {
      PARTITIONS_Z, BASE_Z, BASE_MARKER_Z, FIELD_Z, MapLayer
}